﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：审计模型
 */
var refreshGirdData; // 更新数据
var companyId = "";

var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 公司选择
            $('#companyId').lrselect({
                type: 'tree',
                //placeholder: '==请选择单位==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function(item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        page.search();
                    }
                }
            });
            $('#companyId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSetFirst();
            $('#category').lrDataItemSelect({
                code: 'AUDIT_MODEL_CATEGORYS', maxHeight: 100, placeholder: "==请选择预警分类==",
                select: function (item) {
                   page.search();
                }
            });

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 执行分析
            $('#lr_excute').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要执行分析？', function (res) {
                        if (res) {
                            learun.postForm2(config.base_server() + 'auditmodels/analyse?companyId=' + companyId + '&modelId=' + keyValue + '&access_token=' + config.getToken().access_token, {}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'companyexcel/export?access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'auditmodels/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "序号", name: "code", width: 40, align: "center"},
                    {label: "模型名称", name: "name", width: 280, align: "left"},
                    {label: "执行计划", name: "cronExpression", width: 140, align: "center"},
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {
                        label: "操作", name: "btn2", width: 180, align: "center",
                        formatter: function (value, row, op, $cell) {
                            $cell.on('click', '.excuteNow', function () {
                                if ($cell.html().trim() == '') {
                                    return false;
                                }
                                var keyvalue = row.id;
                                learun.layerConfirm('是否确认立即执行？', function (res, _index) {
                                    if (res) {
                                        learun.loading(true, '执行中...');
                                        learun.httpAsync('Post', config.base_server() + 'auditmodels/analyse?companyId=' + companyId + '&modelId=' + keyvalue + '&access_token=' + config.getToken().access_token, {}, function (data) {
                                            learun.loading(false);
                                                learun.alert.success('执行完成');
                                        });
                                        top.layer.close(_index);
                                    }
                                });
                                return false;
                            });
                            return '<span class="excuteNow" style="cursor: pointer;"><i class="fa fa-play" style="color:green;"></i>立即执行</span>';
                        }
                    },
                    {
                        label: "执行日志", name: "btn1", width: 120, align: "left",
                        formatter: function (value, row, op, $cell) {
                            $cell.on('click', '.viewdetail', function () {
                                if ($cell.html().trim() == '') {
                                    return false;
                                }
                                var keyvalue = row.id;
                                if (learun.checkrow(keyvalue)) {
                                    learun.layerForm({
                                        id: 'form',
                                        title: '查看执行日志',
                                        btn: null,
                                        url: config.webportal_server + 'pages/finance/audit_warn/log/detail.html?companyId=' + companyId + '&keyValue=' + keyvalue + '&access_token=' + config.getToken().access_token,
                                        width: 1366,
                                        height: 600
                                    });
                                }
                                return false;
                            });
                            return '<span class="label label-info viewdetail" style="cursor: pointer;">查看执行日志</span>';
                        }
                    },
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable').jfGridGet('rowdata');
                    var keyValue = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '编辑预警规则',
                            url: config.webportal_server + 'pages/finance/audit_rule/form.html?access_token=' + config.getToken().access_token,
                            width: 550,
                            height: 580,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isTree: false,
                mainId: 'id',
                isPage: true,
                isMultiselect: true,
                reloadSelected: true,
            });
            page.search();
        },
        search: function (param) {
            param = param || {};

            var companyId = $("#companyId").lrselectGet();
            var category = $("#category").lrselectGet();

            param.companyId = companyId;
            param.category = category;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


